package com.sq.partner.manager.webservice;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.log4j.Logger;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.sso.webservice.ServiceException_Exception;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.webservice.receiptandpayment.NewIServicePortType;

public class ReceiptAndPaymentClient {
	private static Logger logger = LogUtil.getLog(ReceiptAndPaymentClient.class);
	private static NewIServicePortType newIServicePortType = null;
	static {
		try {
			String url = Config.getProp(Constants.NLIFE_TO_PAY_WEBSERVICE_URL);
			logger.info("收付webservice请求地址:" + url);
			// 创建WebService客户端代理工厂
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
			// 注册WebService接口
			factory.setServiceClass(NewIServicePortType.class);
			// 设置WebService地址
			factory.setAddress(url);
			newIServicePortType = (NewIServicePortType) factory.create();
			setTimeoutTime(newIServicePortType);
		} catch (Exception e) {
			logger.error("收付webservice URL 初使化失败", e);
		}
		logger.info("收付webservice URL 初使化成功");
	}
	/**
	* 设置web service 连接超时和读取超时
	* @param service
	*/
	private static void setTimeoutTime(Object service){
        Client proxy = ClientProxy.getClient(service);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        try{
            policy.setConnectionTimeout(Integer.parseInt(Config.getProp("connectionTimeout")));
            policy.setReceiveTimeout(Integer.parseInt(Config.getProp("receiveTimeout")));
        }catch(Exception e){
            policy.setConnectionTimeout(120000);
            policy.setReceiveTimeout(120000);
            logger.error(e);
        }
        conduit.setClient(policy);
	}

	/**
	 * CIC报文发送
	 * @param requestXml
	 * @return
	 * @throws ServiceException_Exception
	 */
	public static String getRequest(String requestXml)
			throws ServiceException_Exception {
		return newIServicePortType.getRequest(requestXml);
	}

	public static void main(String[] args) throws Exception {
		String requestXml = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n"
		        + "<INSUREQ>\n"
		        + "    <HEAD>\n"
		        + "        <USER_NAME>ECUser</USER_NAME>\n"
		        + "        <USER_PSW>EC100</USER_PSW>\n"
		        + "    </HEAD>\n"
		        + "    <MAIN>\n"
		        + "        <TRANSRNO>2103</TRANSRNO>\n"
		        + "        <SERIALNUMBER>JHK2018102614200530</SERIALNUMBER>\n"
		        + "        <TRANSRDATE>2018-10-26 14:20:05</TRANSRDATE>\n"
		        + "    </MAIN>\n"
		        + "    <PAYINFO>\n"
		        + "        <C_PAYSEQ_NO>CF20181016164046PAY0001</C_PAYSEQ_NO>\n"
		        + "        <C_ITEM_NO>1</C_ITEM_NO>\n"
		        + "        <N_AMT>0.01</N_AMT>\n"
		        + "        <C_CUR_NO>01</C_CUR_NO>\n"
		        + "        <C_DPT_CDE>21130409</C_DPT_CDE>\n"
		        + "        <C_BAL_TYPE>019</C_BAL_TYPE>\n"
		        + "        <C_IS_AUTOCAV>Y</C_IS_AUTOCAV>\n"
		        + "        <T_UPD_TM>2018-10-26 14:20:05</T_UPD_TM>\n"
		        + "        <C_UPD_CDE>51202367</C_UPD_CDE>\n"
		        + "    </PAYINFO>\n"
		        + "    <BASE>\n"
		        + "        <BASE_LIST>\n"
		        + "            <C_CAR_NO>18210364000048</C_CAR_NO>\n"
		        + "            <C_RCPT_NO>CF20181016164046</C_RCPT_NO>\n"
		        + "            <C_ITEM_NO>1</C_ITEM_NO>\n"
		        + "            <N_AMT>0.01</N_AMT>\n"
		        + "            <C_DPT_CDE>21130409</C_DPT_CDE>\n"
		        + "            <C_CRT_CDE>51202367</C_CRT_CDE>\n"
		        + "            <C_CUR_NO>01</C_CUR_NO>\n"
		        + "            <C_BSNS_TYP>A0301</C_BSNS_TYP>\n"
		        + "            <C_CHA_CLS>A03</C_CHA_CLS>\n"
		        + "            <C_CHA_CDE>A</C_CHA_CDE>\n"
		        + "            <C_SLS_CDE>21130407</C_SLS_CDE>\n"
		        + "        </BASE_LIST>\n"
		        + "    </BASE>\n"
		        + "</INSUREQ>";
		System.out.println(getRequest(requestXml));

	}
}
